package com.xxl.job.admin.feign;

import com.xxl.job.admin.controller.annotation.PermissionLimit;
import com.xxl.job.admin.core.model.XxlJobInfo;
import com.xxl.job.admin.service.XxlJobService;
import com.xxl.job.core.biz.model.ReturnT;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RestController;
import top.lixunda.center.xxljob.api.dto.CopyReturnT;
import top.lixunda.center.xxljob.api.dto.CopyXxlJobInfo;
import top.lixunda.center.xxljob.api.feign.IFeignJobInfoController;
import top.lixunda.ecommerce.server.business.BaseController;

import javax.annotation.Resource;

/**
 * 任务管理接口
 *
 * @author lixunda
 * @version time:2020/4/30 11:55
 */
@Slf4j
@RestController
public class FeignJobInfoController extends BaseController
        implements IFeignJobInfoController {

    @Resource
    private XxlJobService xxlJobService;

    /**
     * 添加工作任务
     *
     * @param jobInfo 工作任务信息
     * @return 返回结果
     */
    @Override
    @PermissionLimit(limit=false)
    public CopyReturnT<String> add(CopyXxlJobInfo jobInfo) {
        XxlJobInfo xxlJobInfo = new XxlJobInfo();
        BeanUtils.copyProperties(jobInfo, xxlJobInfo);
        ReturnT<String> temp = xxlJobService.add(xxlJobInfo);
        CopyReturnT<String> result = new CopyReturnT<>();
        BeanUtils.copyProperties(temp, result);
        return result;
    }

    /**
     * 更新工作任务
     *
     * @param jobInfo 工作任务信息
     * @return 返回结果
     */
    @Override
    @PermissionLimit(limit=false)
    public CopyReturnT<String> update(CopyXxlJobInfo jobInfo) {
        XxlJobInfo xxlJobInfo = new XxlJobInfo();
        BeanUtils.copyProperties(jobInfo, xxlJobInfo);
        ReturnT<String> temp = xxlJobService.update(xxlJobInfo);
        CopyReturnT<String> result = new CopyReturnT<>();
        BeanUtils.copyProperties(temp, result);
        return result;
    }

    /**
     * 移除工作任务
     *
     * @param id 工作任务编号
     * @return 返回结果
     */
    @Override
    @PermissionLimit(limit=false)
    public CopyReturnT<String> remove(int id) {
        ReturnT<String> temp = xxlJobService.remove(id);
        CopyReturnT<String> result = new CopyReturnT<>();
        BeanUtils.copyProperties(temp, result);
        return result;
    }

    /**
     * 开启工作任务
     *
     * @param id 工作任务编号
     * @return 返回结果
     */
    @Override
    @PermissionLimit(limit=false)
    public CopyReturnT<String> start(int id) {
        ReturnT<String> temp = xxlJobService.start(id);
        CopyReturnT<String> result = new CopyReturnT<>();
        BeanUtils.copyProperties(temp, result);
        return result;
    }

    /**
     * 停止工作任务
     *
     * @param id 工作任务编号
     * @return 返回结果
     */
    @Override
    @PermissionLimit(limit=false)
    public CopyReturnT<String> stop(int id) {
        ReturnT<String> temp = xxlJobService.stop(id);
        CopyReturnT<String> result = new CopyReturnT<>();
        BeanUtils.copyProperties(temp, result);
        return result;
    }
}
